package com.zuo.demo01.service.impl;


import com.zuo.demo01.dao.BlogTagRelationMapper;
import com.zuo.demo01.dao.TagMapper;
import com.zuo.demo01.pojo.BlogTagRelation;
import com.zuo.demo01.pojo.Tag;
import com.zuo.demo01.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;


    @Autowired
    private BlogTagRelationMapper blogTagRelationMapper;

    @Override
    public List<Tag> findAll(Map<String, Integer> map) {
        return tagMapper.findAll(map);
    }

    @Override
    public List<Tag> findAll() {
        return tagMapper.findAll1();
    }

    @Override
    public List<Tag> findByBlog(Integer id) {
        List<Tag> tags = new ArrayList<>();
        List<BlogTagRelation> btrs = blogTagRelationMapper.findByBlog(id);
        for (BlogTagRelation btr :
                btrs) {
            tags.add(btr.getTag());
        }

        return tags;
    }


    @Override
    public Tag selectByName(String name) {
        return tagMapper.selectByName(name);
    }

    @Override
    public int count() {
        return tagMapper.count();
    }

    @Override
    public int insert(Tag tag) {
        Tag tag1 = selectByName(tag.getName());
        if(tag1==null){
            Date date = new Date(System.currentTimeMillis());
            tag.setCreateTime(date);
            return tagMapper.insert(tag);
        }
        return 0;
    }

    @Override
    public int delete(int id) {
        return tagMapper.delete(id);
    }


}
